package com.jwater.core.utils;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jwater.org.springframework.core.io.Resource;
import jwater.org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import jwater.org.springframework.core.io.support.ResourcePatternResolver;

/**
 * @description:
 * @author: jnner
 * @createDate: 2021/2/5
 * @version: 1.0
 */
public class ResourceUtils {
    protected static Logger logger = LoggerFactory.getLogger(ResourceUtils.class);

    /**
     * 获取资源
     *
     * @param locationPattern
     * @return
     * @throws IOException
     */
    public static Resource[] getResources(String locationPattern) throws IOException {
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource resources[] = resolver.getResources(locationPattern);
        if (resources != null) {
            return resources;
        }
        return new Resource[0];
    }

    /**
     * 获取资源
     *
     * @param locationPattern
     * @return
     * @throws IOException
     */
    public static Resource getResource(String locationPattern) throws IOException {
        Resource resources[] = getResources(locationPattern);
        if (resources.length == 0) {
            return null;
        }
        if (resources.length > 1) {
            logger.error("find file gt 1 for:" + locationPattern);
        }
        return resources[0];
    }
}
